<?php
class ProfileController extends Controller
{
	public $layout = '//layouts/column1';
	
	public function init()
	{
		// Guest will be redirect to home URL
		if(Yii::app()->user->isGuest) Yii::app()->user->loginRequired();
		parent::init();
	}

	public function actionIndex()
	{
		$this->render('index', array('model'=>me()));
	}

	/**
	 * This function due with $first_name, $last_name and $name attribute of user
	 *
	 * @var string
	 **/
	public function actionName()
	{
		$model = me();

		if(isset($_POST['User']))
	    {
	        $model->first_name = $_POST['User']['first_name'];
	        $model->last_name = $_POST['User']['last_name'];
	        switch($_POST['User']['name'])
	        {
	        	case 0: // first + last
	        		$model->name = $model->first_name . ' ' . $model->last_name; break;
	        	case 1: // last + first
	        		$model->name = $model->last_name . ' ' . $model->first_name; break;
	        	case 2: // first
	        		$model->name = $model->first_name; break;
	        	case 3: // last
	        		$model->name = $model->last_name; break;
	        }
	        if($model->validate())
				$model->update(array('first_name','last_name','name'));
		}
		$this->setEnvAjaxCall();
		$this->render('name',array('model'=>$model));
	}


	/* Allow user to choose which name will be used as url to his homepage */
	public function actionUsername()
	{

	}

	/* Temporary act as a redirect action due to missing #hash in URL after login */
	public function actionChangePassword()
	{
		$this->redirect(array('profile/','#'=>'password'));
	}
	/**
	 * Allow changing user's password
	 *
	 * @var int
	 **/
	public function actionPassword()
	{
				/*
		$model = new ChangePasswordForm;

	    if(isset($_POST['ChangePasswordForm']))
	    {
	        $model->attributes=$_POST['ChangePasswordForm'];
	        if($model->validate())
	        {
	            // form inputs are valid, do something here
				$user = me();
				if($user->checkPassword($model->old_password))
				{
					$user->user_pass = User::EncryptPassword($model->password);
					$user->update(array('user_pass'));
					user()->setFlash('message',Yii::t('user','Cập nhật mật khẩu thành công!'));
					$this->refresh();
				}
				else
				{
					$model->addError('old_password',Yii::t('user','Mật khẩu cũ không chính xác, vui lòng nhập lại.'));
				}
	        } 
	    }
		$model->unsetAttributes();
		$this->render('changePassword', array('model'=>$model));
		*/
	}

	/**
	 * Allow changing user's emails
	 *
	 * @var int
	 **/
	public function actionEmail()
	{

	}

	/**
	 * Allow changing user's other information
	 *
	 * @var int
	 **/
	public function actionProfile()
	{

	}

	/** TODO: this function will due with meta datas if any **/
	public function actionUpdate()
	{
		// get current user
		$model = me();
		
		// load his public profile from meta table
		$meta = new PublicProfileMetaForm;
		foreach ($meta->attributes as $key => $value) {
			$meta->{$key} = $model->getMeta($key, NULL);
		}
		
		$isUpdate = false;

		// update user's display name only
	    if(isset($_POST['User']))
	    {
	        $model->attributes=$_POST['User'];
			$isUpdate = $model->update(array('first_name','last_name'));
		}
		
		// update mobile, public_profile, realname
		if(isset($_POST['PublicProfileMetaForm']))
	    {
	        $meta->attributes = $_POST['PublicProfileMetaForm'];
			if($valid = $meta->validate()) {
				foreach ($meta->attributes as $key => $value) {
					$model->setMeta($key,$value);
				}
			}
	    }
		$isUpdate ?  
			$this->refresh() : 
	    	$this->render('update',array('model'=>$model,'meta'=>$meta));
	}
	
	
	public function actionChangeAvatar()
	{
		Yii::import("ext.xupload.models.XUploadForm");
		$model = new XUploadForm;
		$model->file = CUploadedFile::getInstance($model, 'avatar');		
		$model->mime_type = $model->file->getType();
		$model->size = $model->file->getSize();
		$model->name = $model->file->getName();
		
		if ($model->validate()) {
			$me = me();
			$me->setAvatar($model->file);
			
			echo CJSON::encode(array(
				"thumbnail"=>$me->avatar,
				"name" => $model->name,
				"type" => $model->mime_type,
				"size"=> $model->getReadableFileSize()
			));
		} else {
			echo CVarDumper::dumpAsString($model->getErrors());
			Yii::log("XUploadAction: ".CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, "application.extensions.xupload.actions.XUploadAction");
			throw new CHttpException(400, Yii::t('user','Không tải được ảnh đại diện, hãy kiểm tra quyền CMOD của thư mục.'));
		}
	}
}